global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"
set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/11_plot_rd_hires_detrended", replace


*********************************************************
*Clean Data
*********************************************************
*For each new hire, identify last observed job
	use "$output\new_hires_panel_wide", clear
	
	*Keep only if it's a previous job
		gen months_diff=mofd(yr_month) - mofd(month_hired)
		gen previous_job=months_diff<=0 & client_id!=client0
		keep if previous_job==1
			
	*Last observed job
		egen months_last_job=max(months_diff), by(emp_pur_c)
		keep if months_last_job==months_diff 
		
		*Keep highest salary if worked two jobs at once previously
		egen base_max=max(base), by(emp_pur_c client0 state0 month_hired)
		keep if base_max==base
		egen gross_max=max(gross), by(emp_pur_c client0 state0 month_hired)
		keep if round(gross_max)==round(gross)
		duplicates drop emp_pur_c client0 state0 month_hired, force
		count 
		
	*Change in base pay from last job 
		gen diff_base=(base0-base)/base
		gen log_base=log(base)
		
	*Drop outliers who get crazy changes in base pay 
		keep if base>100 & base<=10000
		drop base_max gross_max months_last_job previous_job
	
	*Probability of observing a previous job 
		merge 1:1 emp_pur_c client0 state0 month_hired using "$temp/hires_temp_wide"
		gen observe=_merge==3 
		drop _merge
		
	*Drop if only temporary layoff 
		duplicates tag emp_pur_c client0, gen(tag)
		egen min_hired=min(month_hired), by(emp_pur_c client0)
		keep if min_hired==month_hired
		
*Create variables for regression
	*Group base pay into bins 
	gen bin=floor((base0-913)/40)*40+913
	gen treat=base0>=913 & base0<=953
	gen above=bin>=913

	*Group months into quarters
	gen temp_months = mofd(month_hired)-mofd(mdy(05,1,2016))
	gen months_bin=floor(temp_months/3)*3
	

*********************************************************
*Fig C1
*********************************************************
	*Program to save estimates 
	cap program drop plot_est
	program define plot_est 
	args spec var name ytitle ytitle2 ylabel ylabel2

		preserve 
			gen pre="pre" if month_hired<= mdy(04,1,2016)
			replace pre="post" if month_hired> mdy(04,1,2016)
			keep if pre=="`spec'"
			keep if bin>=513 & bin<=1313
		
			*Detrend
			reg `var' base0
			predict `var'_res, residual
			predict `var'_pred
			
			*Graph the raw figures 
			collapse `var' `var'_pred `var'_res, by(bin)
			
			graph twoway (scatter `var' bin, lc(black) mcolor(black) legend(off)) ///
				(line `var'_pred bin, legend(off)), ///
				xline(913) xtitle("Weekly Base Pay at Hire") ///
				ytitle(`ytitle') ylabel(`ylabel') legend(off)
			graph export "$graphs/`name'_means.eps", replace
			graph export "$graphs/`name'_means.jpg", replace
			
			graph twoway (scatter `var'_res bin, lc(black) mcolor(black) legend(off)), ///
				xline(913) xtitle("Weekly Base Pay at Hire") ///
				yline(0) ytitle(`ytitle') ylabel(`ylabel2') legend(off)
			graph export "$graphs/`name'_detrended_means.eps", replace
			graph export "$graphs/`name'_detrended_means.jpg", replace
		
		restore
	end 
	
	*Fig C1
	foreach spec in pre post {
		plot_est `spec' base figc1_raw_base_rd_`spec' "Weekly Base Pay at Previous Job" 500(100)1200
		plot_est `spec' log_base figc1_raw_log_base_rd_`spec' "Log Weekly Base Pay at Previous Job"
		plot_est `spec' diff_base figc1_raw_diff_base_rd_`spec' "% Change in Weekly Base Pay from Previous Job" 0.1(0.1)0.6
		plot_est `spec' months_diff figc1_raw_months_diff_rd_`spec' "Months Since Previous Job" 
		plot_est `spec' observe figc1_raw_observe_rd_`spec' "Probability Observe Any Previous Job" 
	}
	
	

*********************************************************
*Repeat for future outcomes post hire
*********************************************************
*For each new hire, identify salary in 18 months
	use "$output\new_hires_panel_wide", clear
	gen months_diff=mofd(yr_month) - mofd(month_hired)
	keep if months_diff==18 & client_id==client0 
	duplicates drop emp_pur_c client0 state0 month_hired, force
	
	*Generate wage growth 
	gen wage_growth=(base-base0)/base0 
	rename base base_future
	gen log_base_future=log(base_future)
	
	*Probability of observing wages 18 month post at same firm
	merge 1:1 emp_pur_c client0 state0 month_hired using "$temp/hires_temp_wide"
	gen separation=_merge==2
	drop _merge
		
*Create variables for regression
	*Group base pay into bins 
	gen bin=floor((base0-913)/40)*40+913
	gen treat=base0>=913 & base0<=953
	gen above=bin>=913
	
	*Group months into quarters
	gen temp_months = mofd(month_hired)-mofd(mdy(05,1,2016))
	gen months_bin=floor(temp_months/3)*3
	
	
*Fig C1:	
	foreach spec in pre post {
		plot_est `spec' base_future figc1_raw_base_future_rd_`spec' "Weekly Base Pay 18 Months After Hire" 
		plot_est `spec' log_base_future figc1_raw_log_base_future_rd_`spec' "Log Weekly Base Pay 18 Months After Hire" 
		plot_est `spec' wage_growth figc1_raw_wage_growth_rd_`spec' "% Change in Weekly Base Pay 18 Months After Hire" 0.05(0.05)0.25
		plot_est `spec' separation figc1_raw_separation_rd_`spec' "Probability of Separation 18 Months After Hire" 0.3(0.05)0.55
	}
	
		

log close